<?php


namespace App\Service\Cooperative;


use App\Dao\TaskPushDao;
use Hyperf\Di\Annotation\Inject;

class TaskPushService extends BaseService
{
    /**
     * @Inject()
     * @var TaskPushDao
     */
    protected $taskPushDao;

    public function index(array $where)
    {
        $where['cooperative_id'] = $this->user()->cooperative_id;
        $pageInfo = $this->extractPaginateInfo($where);
        $index = $this->taskPushDao
            ->where($where)
            ->with(['user', 'task'])
            ->paginate(
                $pageInfo['perPage'],['*'],'page',$pageInfo['page']
            );

        return $this->formatIndex($index);
    }

    public function formatIndex(\Hyperf\Contract\LengthAwarePaginatorInterface $index)
    {
        $data = [];
        /** @var \App\Model\TaskPush $taskPush */
        foreach ($index->items() as $taskPush) {
            $data[] = [
                'task_id' => $taskPush->task->id,
                'user_name' => $taskPush->user->nickname,
                'user_phone' => $taskPush->user->phone,
                'task_price' => $taskPush->task->price,
                'task_title' => $taskPush->task->title,
                'created_at' => $taskPush->created_at->toDateTimeString()
            ];
        }
        return [
            'data' => $data,
            'current_page' => $index->currentPage(),
            'total' => $index->total(),
            'per_page' => $index->perPage()
        ];
    }
}